const service = createWebService('http://example.com/api')

const track = (method,uri) => { console.log(`[${Date.now()}]---method:${method}-uri:${uri}`)}


const httpHandler = (baseUrl, method, path, opts) => {
    const uri = baseUrl + '/' + path;
    track(method, uri)
    return http[method](uri, opts)
}

class HttpHandler {
    constructor(baseUrl) {
        this.baseUrl = baseUrl;
    }
    handle(path, opts) {
        const { baseUrl, method } = this;
        const uri = baseUrl + '/' + path;
        track(method, uri)
        return http[method](uri, opts)
    }
}

function createWebService(baseUrl) {
    const httpHandler = new HttpHandler(baseUrl)
    return new Proxy({}, {
        get(target, propKey, receiver) {
            const method = propKey.toUpperCase();
            if (['GET', 'POST', 'DELETE', 'UPDATE', 'OPTIONS'].includes(method)) {
                // return httpHandler.bind(null, baseUrl, method);
                httpHandler.method = method;
                return httpHandler.handle
            } else {
                throw new Error(`${propKey} should be a valid value like [get,post,delete,update]`)
            }
        }
    })
}

function getList() {
    return service.get('/admin/list', {})
}